package Test;

import java.util.HashMap;

public class 和为K的子数组 {
    public static int subarraySum(int[] nums, int k) {
       HashMap<Integer, Integer> hashMap=new HashMap<>();
       hashMap.put(0,1);
       int sum=0;
       int res=0;
       for (int i=0;i<nums.length;i++){
           sum+=nums[i];
           int sum2=sum-k;
           if(hashMap.containsKey(sum2)) res+=hashMap.get(sum2);
           hashMap.put(sum,hashMap.getOrDefault(sum,0)+1);
       }
       return res;
    }

    public static void main(String[] args) {
        int[] num={1,1,1};
        System.out.println(subarraySum(num,2));
    }
}
